package com.gan.springbootdemo.application;

import com.gan.springbootdemo.application.dto.Device;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(description = "设备相关接口")
@Slf4j
@RestController
@RequestMapping("device")
public class DeviceController {

    private Device device = new Device();

    @ApiOperation("查询所有设备信息")
    @GetMapping("/getMap")
    public Map getMap() {
        Map map = new HashMap();
        map.put("id", 123);
        map.put("name", "box123");
        map.put("deviceId", "100852321601");
        map.put("device", device);
        return map;
    }

    @ApiOperation("根据name查询单台设备信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "设备主键", dataType = "String", required = true, paramType = "path")})
    @GetMapping("selectByName/{name:.+}")
    public Device getDevice(@PathVariable String name) {
        log.info("根据name查询单台设备信息: name = {}", name);
        return null;
    }

    @ApiOperation("根据id查询单台设备信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "设备主键", dataType = "int", required = true, paramType = "path")})
    @GetMapping("selectById/{id}")
    public Device getDevice2(@PathVariable Integer id) {
        log.info("根据id查询单台设备信息: id = {}", id);
        if (device.getId().equals(id)) {
            return device;
        }
        return null;
    }

    @ApiOperation("添加设备")
    @PostMapping
    public Device addDevice(@RequestBody Device device) {
        this.device = device;
        log.info("添加设备: {}", this.device.toString());
        return this.device;
    }

    @ApiOperation("添加设备")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "设备主键", dataType = "int", required = true, paramType = "path"),
            @ApiImplicitParam(name = "name", value = "设备名称", dataType = "String", required = true, paramType = "path"),
            @ApiImplicitParam(name = "deviceId", value = "设备序列号", dataType = "String", required = true, paramType = "path")})
    @PostMapping("/{id}/{name}/{deviceId}")
    public Device addDevice2(@PathVariable Integer id, @PathVariable String name, @PathVariable String deviceId) {
        log.info("添加设备 id: {}", id);
        log.info("添加设备 name: {}", name);
        log.info("添加设备 deviceId: {}", deviceId);
        device.setId(id);
        device.setName(name);
        device.setDeviceId(deviceId);
        log.info("添加设备 device: {}", device.toString());
        return device;
    }

    @ApiOperation("修改设备信息")
    @PutMapping
    public Device updateDevice(@RequestBody Device device) {
        this.device = device;
        log.info("修改设备信息: {}", this.device.toString());
        return this.device;
    }

    @ApiOperation("删除设备")
    @DeleteMapping
    public int deleteDevice(@RequestBody Device device) {
        log.info("删除设备: {}", device.toString());
        return 200;
    }

    @ApiOperation("根据主键删除设备")
    @DeleteMapping("/deleteByKey")
    public void deleteDevice2(Integer[] key) {
        for (Integer i : key) {
            log.info("key[]: {}", i);
        }
    }

    @ApiOperation("上传文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "usrKey", value = "用户id", dataType = "int", required = true, paramType = "path"),
            @ApiImplicitParam(name = "path", value = "上传路径", dataType = "String", required = true, paramType = "query")})
    @PostMapping("/uploadFile/{usrKey}")
    public void uploadFile(@PathVariable Integer usrKey, String path) {
        log.info("usrKey: {}", usrKey);
        log.info("path: {}", path);
    }

}
